package cn.wolfcode.core;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@Component
@ServerEndpoint("/seckill/{token}")
public class SeckillWebSocketEndPointer {
    public static  final Map<String, Session>  SESSION_MAP = new ConcurrentHashMap<>();

    @OnOpen
    public void onOpen(@PathParam("token")String token ,Session session){
        log.info("[Server] 有新的秒杀请求，客户端：{}",token);
        SESSION_MAP.put(token, session);
    }

   /* @OnMessage
    public void onMessage(@PathParam("token")String token, String msg){
        log.info("[Server] 收到客户端发送的消息：clientId={}，msg={} ",token,msg);
    }*/

    @OnClose
    public void onClose(@PathParam("token")String token){
        log.info("[Server] 客户端{}断开连接",token);
    }
    @OnError
    public void onError(@PathParam("token")String token,Throwable throwable){
        log.info("[Server] 客户端连接出现异常...",throwable);
        SESSION_MAP.remove(token);
    }

}
